package com.frinder.base.wx;

import com.frinder.base.akka.BaseActor;
import com.frinder.base.akka.MyLookupEventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

/**
 * @ClassName SystemMessageActor
 * @Description //TODO
 * @Author frinder
 * @Date 2019/1/19 17:52
 * @Version 1.0
 */
@Component
@Scope("prototype")
public class SystemMessageActor extends BaseActor {

    public static final String NAME = "system-message-actor";

    @Autowired
    private SimpMessagingTemplate template;

    @Override
    public void preStart() throws Exception {
        super.preStart();
        MyLookupEventBus.BUS.subscribe(getSelf(), MyLookupEventBus.Topic.SYSTEM_MESSAGE_EVENT);
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(SystemMessageEvent.class, this::push)
                .build();
    }

    private void push(SystemMessageEvent event) {
        log().info("推送消息：{}", event);
        template.convertAndSend("/topic/message", event.getMessage());
    }

}
